package cn.itcast.bos.web.action.take_delivery;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

public class WayBillAction extends BaseAction<WayBill> {
	private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);
	@Resource
	private WayBillService service;

	// 保存快速录入运单
	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		Map<String, Object> results = new HashMap<>();
		try {
			service.save(model);
			// 保存成功
			results.put("success", true);
			results.put("msg", "保存运单成功!");
			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());
		} catch (Exception e) {
			results.put("success", false);
			results.put("msg", "保存运单失败!");
			LOGGER.info("保存运单失败，运单号：" + model.getWayBillNum(), e);
		}
		ActionContext.getContext().getValueStack().push(results);
		return SUCCESS;
	}

	// 无条件分页查询运单
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pagequery() {
		// 无条件查询(根据id排序)
		Pageable pageable = new PageRequest(page -1, rows);
		Page<WayBill> page = service.findpageQuery(model,pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}

	// 快速录入的运单的回显功能
	@Action(value = "order_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {
		// 设置回显的结果集
		Map<String, Object> results = new HashMap<>();
		// 调用业务层查询该单号对应的运单信息
		WayBill wayBill = service.findByWayBillNum(model.getWayBillNum());
		// 判断return的对象是否为空
		if (wayBill == null) {
			results.put("success", false);
		} else {
			if (wayBill.getSignStatus() != 1) {
				results.put("success", false);
				results.put("msg", "此运单已经生成,无法修改保存");
			} else {
				results.put("success", true);
				results.put("waybillData", wayBill);
			}
		}
		ActionContext.getContext().getValueStack().push(results);
		return SUCCESS;
	}

	// 保存完整运单
	@Action(value = "waybill_saveAll", results = { @Result(name = "success", type = "json") })
	public String saveWayBill() {
		// 设置回显的结果集
		Map<String, Object> results = new HashMap<>();
		// 解决持久态对象关联了一个瞬时态对象的问题
		if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder().getId() == 0)) {
			model.setOrder(null);
		}
		try {
			service.save(model);
			// 保存成功
			results.put("success", true);
			results.put("msg", "保存运单成功!");
			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());
		} catch (Exception e) {
			results.put("success", false);
			results.put("msg", "保存运单失败!");
			LOGGER.info("保存运单失败，运单号：" + model.getWayBillNum(), e);
		}
		ActionContext.getContext().getValueStack().push(results);
		return SUCCESS;
	}
}
